package com.msh.artascope.devcloud.service.service.impl;


import com.msh.artascope.devcloud.service.service.EnvService;
import com.msh.artascope.devcloud.service.dao.JenkinsConfigDao;
import com.msh.artascope.devcloud.service.model.query.JenkinsConfigQueryVo;
import com.msh.artascope.devcloud.service.model.vo.JenkinsConfigVo;
import com.msh.frame.client.base.TenantBaseServiceImpl;
import com.msh.frame.common.util.StringUtil;
import javax.annotation.Resource;

import com.msh.frame.interfaces.IdGenerateable;
import com.msh.artascope.devcloud.client.po.JenkinsConfigPo;
import com.msh.artascope.devcloud.client.qo.JenkinsConfigQo;
import com.msh.artascope.devcloud.service.service.JenkinsConfigService;
import com.msh.frame.client.common.Result;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Service
@Slf4j
public class JenkinsConfigServiceImpl extends TenantBaseServiceImpl<JenkinsConfigPo,JenkinsConfigQo> implements JenkinsConfigService {

  @Resource
  private EnvService envService;

  @Resource
  private JenkinsConfigDao jenkinsConfigDao;

  @Resource
  private IdGenerateable idGenerateable;

  @Override
  public long insert(JenkinsConfigPo param) {
    param.setId(idGenerateable.getUniqueId());
    return super.insert(param);
  }

  @Override
  public int insertCollection(Collection<JenkinsConfigPo> param) {
    param.stream().forEach(p->p.setId(idGenerateable.getUniqueId()));
    return super.insertCollection(param);
  }

  @Override
  public JenkinsConfigPo getByEnvId(Long envId, Long tenantId) {
    JenkinsConfigPo jenkinsConfigPo = listOne(
            new JenkinsConfigQo().setEnvId(envId).setTenantId(tenantId));
    return jenkinsConfigPo;
  }

  @Override
  public void update(JenkinsConfigPo param) {
    Long id = param.getId();
    Long tenantId = param.getTenantId();
    JenkinsConfigPo jenkinsConfigPo = this.get(id, tenantId);
    String usernameDb = jenkinsConfigPo.getUsername();
    String passwordDb = jenkinsConfigPo.getPassword();
    if(StringUtil.desensitize(usernameDb, 1,1).equals(param.getUsername()) || StringUtils.isEmpty(param.getUsername())){
      param.setUsername(null);
    }
    if(StringUtil.desensitize(passwordDb, 1,1).equals(param.getPassword()) || StringUtils.isEmpty(param.getPassword())){
      param.setPassword(null);
    }
    super.update(param);
  }

  // ----------前端vo处理-------------------------------------------------------

  private JenkinsConfigVo toVo(JenkinsConfigPo po){
    JenkinsConfigVo vo = po.convert(JenkinsConfigVo.class);
    vo.setPassword(StringUtil.desensitize(vo.getPassword(), 1, 1));
    vo.setUsername(StringUtil.desensitize(vo.getUsername(), 1, 1));
    return vo;
  }

  private List<JenkinsConfigVo> toVoList(List<JenkinsConfigPo> list){
    if(CollectionUtils.isEmpty(list)){
      return Collections.emptyList();
    }

    return list.stream().map(po->{
      return toVo(po);
    }).collect(Collectors.toList());
  }

  @Override
  public JenkinsConfigVo getVo(Long id, Long tenantId) {
    JenkinsConfigPo po = this.get(id, tenantId);
    if(null == po){
      return null;
    }
    JenkinsConfigVo vo = toVo(po);

    return vo;
  }

  @Override
  public List<JenkinsConfigVo> listVo(JenkinsConfigQueryVo q){
    JenkinsConfigQo qo = q.convert(JenkinsConfigQo.class);
    List<JenkinsConfigPo> poList = this.list(qo);
    List<JenkinsConfigVo> voList = toVoList(poList);

    return voList;
  }

  @Override
  public Result<List<JenkinsConfigVo>> pageVo(JenkinsConfigQueryVo param) {
    JenkinsConfigQo qo = param.convert(JenkinsConfigQo.class);
    int count = this.count(qo);
    if (0 == count) {
      return Result.successReturn(Collections.<JenkinsConfigVo>emptyList()).setCount(count);
    }
    List<JenkinsConfigPo> poList = this.list(qo);
    List<JenkinsConfigVo> voList = toVoList(poList);

    return Result.successReturn(voList).setCount(count);
  }


}
